package com.fenghongzhang.day004;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

//自定义文件控件
public class FentTextView extends View {

    int mRadius ;
    int mWidth;
    int mHeight;
    int x;
    int y;



    private String text;
    private float size;

    int measureWidth;
    int measureHeight;
    private static final String TAG = "FentTextView";

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    //new用
    public FentTextView(Context context) {
        super(context);
    }

    //xml里面用
    public FentTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        /**
         * 第一个参数attrs
         * 第二个参数是自定义view的名称
         */
//        //取到所有属性值
//        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.FentTextView);
//        //取到文本值
//        text = typedArray.getString(R.styleable.FentTextView_android_text);
//        //获取大小
//        size = typedArray.getDimension(R.styleable.FentTextView_size, 20);

    }

    //设置最重要的属性之一
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        //获取测量模式
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        //获取测量大小
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        //如果为确定大小值，则圆的半径为宽度/2
        if (widthMode == MeasureSpec.EXACTLY && heightMode == MeasureSpec.EXACTLY) {
            mRadius = widthSize / 2;
            x = widthSize / 2;
            y = heightSize / 2;
            mWidth = widthSize;
            mHeight = heightSize;
        }
        //如果为wrap_content 那么View大小为圆的半径大小*2
        if(widthMode == MeasureSpec.AT_MOST&&heightMode ==MeasureSpec.AT_MOST){
            mWidth = (int) (mRadius*2);
            mHeight = (int) (mRadius*2);
            x = mRadius;
            y = mRadius;

        }
        //设置视图的大小
        setMeasuredDimension(mWidth,mHeight);

//        setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
//                getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));

//        //100为宽度默认值
//        measureWidth = getMyViewSize(300, widthMeasureSpec);
//        //200为高度默认值
//        measureHeight = getMyViewSize(300, heightMeasureSpec);
//        //记录子子布局确认后的尺寸
//        setMeasuredDimension(measureWidth, measureHeight);

//        int widthModel = MeasureSpec.getMode(widthMeasureSpec);
//        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
//
//        int heightModel = MeasureSpec.getMode(heightMeasureSpec);
//        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
//        setMeasuredDimension(width,height);

    }

    private int getMyViewSize(int defaultSize, int measureSpec) {
        int result = defaultSize;
        //解封模式和尺寸值
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        switch (specMode) {
            case MeasureSpec.UNSPECIFIED:
                result = defaultSize;
                break;
            case MeasureSpec.AT_MOST:
                //指定一个值
                result = defaultSize;
                break;
            case MeasureSpec.EXACTLY:
                result = specSize;
                break;
        }
        return result;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //设置字体大小
//        paint.setTextSize(size);
//        canvas.drawText(text,measureWidth/2,measureHeight/2,paint);
        canvas.drawColor(Color.RED);
    }
}
